import java.util.PriorityQueue;

/**
 * Created by xiebang
 * Description
 * User:小谢
 * Date:2021-10-25
 * Time:10:19
 */

/*
二叉搜索树：
若左子树不为空，左子树的值小于根的值
若右子树不为空，右子树的值大于根的值
完全二叉树 时间复杂度为O（logn）
最坏的情况：右单支时间复杂度为O（n）
查找：最坏情况下查找的就是9时间复杂度O（logn）
 */
public class BinarySearchTree {

    static class TreeNode{
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val){
            this.val = val;

        }
    }

    public TreeNode root;
    //查找
    public TreeNode search(int key){
        TreeNode cur = this.root;

        while (cur != null) {
            if (cur.val > key) {
                cur = cur.left;
            } else if (cur.val < key) {
                cur = cur.right;
            } else {
                return cur;
            }
        }
        return null;
    }
    //插入，每次插入一个数据一定是在叶子节点的地方
    public void insertTree(int val){
        if (root == null){
            root = new TreeNode(val);
            return;
        }

        TreeNode cur = this.root;
        TreeNode prve = null;
        while (cur!= null){
            prve = cur;
            if(cur.val < val){

                cur = cur.right;
            }else /*if (cur.val > val)*/{

                cur = cur.left;
            }
        }
        if (prve.val < val) {
            prve.right = new TreeNode(val);

        }else {
            prve.left = new TreeNode( val);
        }
    }

    /**
     * 1.不存在
     * 2.不是根节点
     * 3.替罪羊的删除办法
     * @param key
     */
    public void remove(int key){
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null){
            if (cur.val < key){
                parent = cur;
                cur = cur.left;
            }else if (cur.val == key){
                removeNode(parent,cur);
                return;
            }else {
                parent = cur;
                cur = cur.right;
            }
        }
    }

    private void removeNode(TreeNode parent, TreeNode cur) {
        if (cur.left == null){
            if (cur == root){
                cur = cur.right;
            }else if (parent.left == cur){
                parent.left = cur.right;
            }else {
                parent.right = cur.right;
            }
        }else if (cur.right == null){
            if (cur == root){
                cur = cur.left;
            }else if (parent.left == cur){
                parent.left = cur.left;
            }else {
                parent.right = cur.left;
            }
        }else {
            //左树找最大的数据或者右树找最小的数据//替罪羊的删除
            TreeNode targetparent = cur;
            TreeNode target = cur.right;

            while (target.left != null){
                targetparent = target;
                target = target.left;
            }

            cur.val = target.val;
            if (targetparent.right == target){
                targetparent.right = target.right;
            }else if (targetparent.left == target){
                targetparent.left = target.right;
            }
        }
    }

    public void inOrder(TreeNode root){
        if (root == null){
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }
    public void prevOrder(TreeNode root){
        if (root == null){
            return;
        }
        System.out.print(root.val + " ");
        prevOrder(root.left);
        prevOrder(root.right);
    }
}

//AvL树平衡二叉树解决二叉搜索树的问题
//红黑树解决Avl树的问题
